package com.world.business.talisman.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.business.area.domain.Area;
import com.world.common.controller.BaseController;
import com.world.common.domain.ResponseData;
import com.world.common.domain.PageResult;
import com.world.common.domain.SuccessResponseData;
import com.world.common.enumeration.DelFlagEnum;
import com.world.business.talisman.domain.Talisman;
import com.world.business.talisman.domain.query.TalismanPageRequest;
import com.world.business.talisman.domain.query.TalismanRequest;
import com.world.business.talisman.domain.vo.TalismanVO;
import com.world.business.talisman.service.ITalismanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 符箓 前端控制器
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-31
 */
@Api(tags = "符箓")
@RestController
@RequestMapping("/talisman")
public class TalismanController extends BaseController<Talisman> {

    @Resource
    private ITalismanService talismanService;

    /**
     * 列表
     *
     * @param request 请求参数
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 11:14
     */
    @ApiOperation("列表")
    @GetMapping("/list")
    public ResponseData<List<TalismanVO>> list(TalismanRequest request) {
        LambdaQueryWrapper<Talisman> queryWrapper = Wrappers.lambdaQuery(Talisman.class);
        queryWrapper.eq(Talisman::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.like(StrUtil.isNotBlank(request.getName()), Talisman::getName, request.getName());
        List<Talisman> list = talismanService.list(queryWrapper);
        List<TalismanVO> vos = BeanUtil.copyToList(list, TalismanVO.class);
        return new SuccessResponseData<>(vos);
    }

    /**
     * 分页-列表
     *
     * @param request 分页请求参数
     * @return {@link ResponseData}
     * @author WangYX
     * @date 2023/07/31 11:29
     */
    @ApiOperation("分页-列表")
    @GetMapping("/page")
    public ResponseData<PageResult<TalismanVO>> page(TalismanPageRequest request) {
        //1.分页条件
        Page<Talisman> page = startPage(request);

        //2.查询条件
        LambdaQueryWrapper<Talisman> queryWrapper = Wrappers.lambdaQuery(Talisman.class);
        queryWrapper.eq(Talisman::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.like(StrUtil.isNotBlank(request.getName()), Talisman::getName, request.getName());
        Page<Talisman> result = talismanService.page(page, queryWrapper);

        //3.结果转化
        PageResult<TalismanVO> pageResult = getPageResult(result, TalismanVO.class);
        return new SuccessResponseData<>(pageResult);
    }
}
